package com.attendance.chdingtask.controller;


import com.alibaba.fastjson.JSONObject;
import com.attendance.chdingtask.config.WebLog;
import com.attendance.chdingtask.service.IMdCallBackService;
import com.attendance.chdingtask.utils.HttpUtil;
import com.attendance.chdingtask.utils.Logger;
import com.attendance.chdingtask.utils.common.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;

/**
 * 魔点注册回调接口
 * @author
 * 主要接收魔点打卡备推送的人员打卡流水记录
 * 推送内容其中包含三个字段：钉钉人员userid、魔点设备SN码、打卡时间recognizeTime
 */
@Controller
@Api(value = "md", description = "魔点回调事件接口", consumes = "application/json;charset=UTF-8")
@RequestMapping("/md")
public class MdCallBackController {


    private static Logger logger=Logger.getLogger(MdCallBackController.class);

    /**
     * 魔点设备主动推送的组织id
     */
    private static  final   String ORGID="1719104692748812288";


    private IMdCallBackService iMdCallBackService;

    @Autowired
    public void setiMdCallBackService(IMdCallBackService iMdCallBackService) {
        this.iMdCallBackService = iMdCallBackService;
    }




    @PostMapping("/callBack")
    @WebLog(description = "魔点注册回调接口")
    @ResponseBody
    public Result mdCallBack(@RequestParam(value="nonce", required=false) String nonce,
                             @RequestParam(value="timestamp", required=false) String timestamp,
                             @RequestParam(value="signature", required=false) String signature,
                             @RequestParam(value="signVersion", required=false) String signVersion,
                             @RequestParam(value="orgId", required=false) String orgId,
                             @RequestBody(required=false) JSONObject json)  {

        if(!ORGID.equals(orgId)){
            logger.error("************回调事件参数信息出错************orgId错误{}：",orgId);
            return Result.error("***********组织id错误************");
        }

        return iMdCallBackService.receiveCallback(nonce, timestamp, signature, signVersion, orgId, json);
    }
}
